package cn.User.dao.impl;

import cn.User.dao.UserDao;
import common.service.MryeDataCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component("userDao")
public class UserDaoImpl implements UserDao{

    @Autowired
    MryeDataCenterService mryeDataCenterService;


    @Override
    public int setUserHistory(Map<String, Object> params) {
        String sql="INSERT INTO YST_USER_BROWSE_HISTORY(USER_BH,HISTORY_URL,HISTORY_GOODS_BH,USER_IP,HISTORY_DURATION,HISTORY_STATE)VALUES(#{YHBH},#{LLURL},#{SPBH},#{YHIP},#{LLSJ},#{SFSC});";
        params.put("sql",sql);
        return mryeDataCenterService.saveSap(params);
    }

    /**
     * 更新用户状态:登陆成功，退出登陆
     * @param params
     * @return
     */
    public int updateUserInfoState(Map<String, Object> params) {
        int type=Integer.parseInt( params.get("type").toString());
        StringBuffer sql=new StringBuffer("UPDATE `52yst`.`yst_user` SET");
        if(type==3){
            sql.append("`USER_QQ_OPENID` = ISNULL( #{qqopenid},USER_QQ_OPENID),");
        }else  if(type==4){
            sql.append("`USER_WEIXIN_OPENID` = ISNULL( #{weixinopenid},USER_WEIXIN_OPENID),");
        }
        sql.append("`USER_IPTOP`=`USER_IP`,`USER_IP` = #{ip}," +
                "`USER_LAST_TIME` = #{lasttime}," +
                "`USER_STATE` = 1 WHERE `USER_BH` = #{ystUsername};");
        params.put("sql",sql);
        return mryeDataCenterService.updateSap(params);
    }
    /**
     * 获取用户信息
     * @param params
     * @return
     */
    public Map<String, Object> getuserinfo(Map<String, Object> params) {
        String sql="SELECT `USER_ID` AS YHID, `USER_BH` AS YHBH, `USER_NAME` AS YHMC, `USER_FULLNAME` AS YHXM, `USER_SEX` AS YHXB, " +
                " `USER_BIRTHDAY` AS YHSR, `USER_PHONE` AS YHDH, `USER_IMG` AS YHTX, `USER_MONEY` AS YHYE, `USER_EMAIL` AS YHYX, `USER_QQ` AS YHQQ," +
                " `USER_WEIXIN` AS YHWX, `USER_IP` AS YHIP, `USER_CREATE_TIME` AS YHZCTIME, `USER_LAST_TIME` AS YHDLTIME, `USER_STATE` AS YHZT " +
                " FROM YST_USER WHERE USER_BH = #{YHBH}";
        params.put("sql",sql);
        return mryeDataCenterService.searchSap(params);
    }

    /**
     *退出登陆
     * @param params
     * @return
     */
    public int logout(Map<String, Object> params) {
        String sql="UPDATE YST_USER SET USER_STATE=0,USER_LAST_TIME=CURRENT_TIMESTAMP() WHERE USER_BH=#{YHBH}";
        params.put("sql",sql);
        return mryeDataCenterService.updateSap(params);
    }
}
